#ifndef Buffer_h
#include "Buffer.h"

Buffer::Buffer(void)
{
	m_mutex = new Mutex();
	m_full = new CountingSemaphore(0);
	m_buffer = new queue<Message*>();
}

Buffer::~Buffer(void)
{
	while(m_buffer->size() > 0)
	{
		Message* m = m_buffer->front();
		delete m;
		m_buffer->pop();
	}
	delete m_mutex;
	delete m_full;
	delete m_buffer;
}

void Buffer::push(Message *m, Thread *t)
{
	m_mutex->lock(t);
	m_buffer->push(m);
	m_mutex->unlock();
	m_full->up(t);
}

Message* Buffer::pop(Thread *t)
{
	m_full->down(t);
	m_mutex->lock(t);
	Message* m = m_buffer->front();
	m_buffer->pop();
	m_mutex->unlock();

	return m;
}

int Buffer::getSize()
{
	return m_buffer->size();
}

#endif
